/*  Copyright 2008 Rene Weiskircher 

    This file is part of SUPA.

    SUPA is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SUPA is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SUPA.  If not, see <http://www.gnu.org/licenses/>.
 */
package supa;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class InputDialog extends javax.swing.JDialog  implements ActionListener  {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel PovExeLabel;
	private JLabel iniFileLabel;
	private JButton exeBrowseButton;
	private JTextField iniFileNameArea;
	private JTextField exePathField;
	private JLabel numProcLabel;
	private JScrollPane jScrollPane;
	private JScrollPane jScrollPane1;
	private JTextField processorField;
	private JButton goButton;
	private JButton iniBrowseButton;
	private static final String BROWSE_INI="browseIniFile";
	private static final String GO_STRING="Go";
	private static final String BROWSE_EXE = "browseExeFile";
	private static final String SELECT = "Select";

	/**
	* Auto-generated main method to display this JDialog
	 * @param args not needed
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				InputDialog inst = new InputDialog(frame);
				inst.setVisible(true);
			}
		});
	}
	/**
	 * Creates the input dialog in the frame given as argument and sets the title
	 * @param frame where the dialog will be created
	 */
	public InputDialog(JFrame frame) {
		super(frame,true);//has to be modal since we need the input
		setTitle("SUPA Input Dialog");
		initGUI();
	}
	
	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
			thisLayout.rowHeights = new int[] {7, 7, 7, 7};
			thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1};
			thisLayout.columnWidths = new int[] {7, 7, 7};
			getContentPane().setLayout(thisLayout);
			{
				PovExeLabel = new JLabel();
				getContentPane().add(PovExeLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				PovExeLabel.setText("Povray Executable");
			}
			{
				iniFileLabel = new JLabel();
				getContentPane().add(iniFileLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				iniFileLabel.setText("Povray Ini File");
			}
			{
				exePathField = new JTextField(15);
				exePathField.setPreferredSize(new java.awt.Dimension(163, 25));
				jScrollPane = new JScrollPane(exePathField,ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
				getContentPane().add(jScrollPane, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				exeBrowseButton = new JButton();
				getContentPane().add(exeBrowseButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				exeBrowseButton.setText("Browse");
				exeBrowseButton.setActionCommand(BROWSE_EXE);
			}
			{
				iniFileNameArea = new JTextField(15);
				iniFileNameArea.setPreferredSize(new java.awt.Dimension(163, 25));
				jScrollPane1 = new JScrollPane(iniFileNameArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
				getContentPane().add(jScrollPane1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				iniBrowseButton = new JButton();
				getContentPane().add(iniBrowseButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				iniBrowseButton.setText("Browse");
				iniBrowseButton.setActionCommand(BROWSE_INI);
			}
			{
				numProcLabel = new JLabel();
				getContentPane().add(numProcLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				numProcLabel.setText("Number of Processes");
			}
			{
				goButton = new JButton();
				getContentPane().add(goButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				goButton.setText("GO");
				goButton.setPreferredSize(new java.awt.Dimension(71, 58));
				goButton.setActionCommand(GO_STRING);
			}
			{
				processorField = new JTextField(" 2",2);
				getContentPane().add(processorField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				processorField.setPreferredSize(new java.awt.Dimension(29, 22));
			}
			goButton.addActionListener(this);
			iniBrowseButton.addActionListener(this);
			exeBrowseButton.addActionListener(this);
			this.setSize(510, 300);
			//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Execututes the file browser or the rendering.
	 * @param e The event happening in the window
	 */
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(GO_STRING)) {
			Supa.setPovrayExecutable(new File(exePathField.getText()));
			Supa.setIniFile(new File(iniFileNameArea.getText()));
			Supa.setProcessNumber(Integer.parseInt(processorField.getText().trim()));
			this.setVisible(false);
			this.dispose();
		}
		if(e.getActionCommand().equals(BROWSE_EXE)) {
			JFileChooser jfc = new JFileChooser();
			jfc.setApproveButtonText(SELECT);
			int returnVal = jfc.showOpenDialog(this);
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				final File selectedFilePath = jfc.getSelectedFile();
				Supa.setPovrayExecutable(selectedFilePath);
				exePathField.setText(selectedFilePath.getAbsolutePath());
			}
		}
		if(e.getActionCommand().equals(BROWSE_INI)) {
			JFileChooser jfc = new JFileChooser();
			jfc.setApproveButtonText(SELECT);
			int returnVal = jfc.showOpenDialog(this);
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				final File selectedFilePath = jfc.getSelectedFile();
				Supa.setIniFile(selectedFilePath);
				iniFileNameArea.setText(selectedFilePath.getAbsolutePath());
			}
			
		}
	}

}
